package com.draw.cli.gui.panel.newUser;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.xml.ws.WebServiceException;

import com.draw.cli.config.ConfigManager;
import com.draw.cli.gui.Loading;
import com.draw.cli.gui.StateBar;
import com.draw.cli.gui.Window;
import com.draw.cli.gui.panel.Refreshable;
import com.draw.cli.webclient.UserClient;
import com.draw.core.User;
import com.draw.webinterface.UserInterface;

public class NewUserPanel extends JPanel implements ActionListener,Refreshable, Runnable {
	private static final long serialVersionUID = -501761634761648404L;

	public static final String USER_CREATED = "L'utilisateur a bien été créé.";
	public static final String USER_EXISTS = "Le nom d'utilisateur spécifié est déjà utilisé.";
	public static final String USER_ERROR = "Erreur lors de la création de l'utilisateur.";
	public static final String PASSWORD_SMALL = "Le mot de passe spécifié est trop court.";
	public static final int PASSWORD_MIN_SIZE = 4;

	private static NewUserPanel instance = new NewUserPanel();

	public static NewUserPanel getInstance() {
		return instance;
	}

	private JTextField usernameField;
	private JTextField passwordField;
	private JButton ok;
	private JButton cancel;
	private Thread thread;

	public NewUserPanel() {
		this.thread = null;

		this.setLayout(new FlowLayout());

		JPanel pan = new JPanel(new GridLayout(3, 2));

		pan.setBorder(BorderFactory
				.createTitledBorder("Création d'un utilisateur"));

		pan.add(new JLabel("Nom d'utilisateur : "));
		this.usernameField = new JTextField();
		this.usernameField.setPreferredSize(new Dimension(100, 10));
		pan.add(this.usernameField);

		pan.add(new JLabel("Mot de passe : "));
		this.passwordField = new JPasswordField();
		this.passwordField.setPreferredSize(new Dimension(100, 10));
		pan.add(this.passwordField);

		this.ok = new JButton("OK");
		this.ok.addActionListener(this);
		pan.add(this.ok);

		this.cancel = new JButton("Annuler");
		this.cancel.addActionListener(this);
		pan.add(this.cancel);

		this.add(pan);
	}

	public void actionPerformed(ActionEvent arg0) {
		if (this.ok.equals(arg0.getSource())) {
			if (this.thread == null) {
				this.thread = new Thread(this);
				this.thread.start();
			}
		} else {
			Window.getInstance().setHome();
		}
	}

	private void create() {
		this.ok.setEnabled(false);
		this.cancel.setEnabled(false);
		User user = new User(this.usernameField.getText(),
				this.passwordField.getText());
		try {
			UserInterface in = UserClient.getInstance();
			if ( this.passwordField.getText().length() < PASSWORD_MIN_SIZE ) {
				JOptionPane.showMessageDialog(this, PASSWORD_SMALL,
						"Création d'utilisateur", JOptionPane.ERROR_MESSAGE);
			} else if (in.getUserByUsername(this.usernameField.getText()).size() > 0 ) {
				JOptionPane.showMessageDialog(this, USER_EXISTS,
						"Création d'utilisateur", JOptionPane.ERROR_MESSAGE);
			} else {
				Loading.start();
				User createdUser = in.createUser(user);
				Loading.stop();
				if (createdUser == null) {
					JOptionPane
							.showMessageDialog(this, USER_ERROR,
									"Création d'utilisateur",
									JOptionPane.ERROR_MESSAGE);
				} else {
					ConfigManager.getInstance().setCurrentUser(createdUser);
					Window.getInstance().setHome();
					JOptionPane.showMessageDialog(this, USER_CREATED,
							"Création d'utilisateur",
							JOptionPane.INFORMATION_MESSAGE);
				}
			}
		} catch (MalformedURLException | WebServiceException e) {
			StateBar.getInstance().setServerOffline();
			JOptionPane.showMessageDialog(this, StateBar.SERVER_OFFLINE,
					"Création d'utilisateur", JOptionPane.ERROR_MESSAGE);
		}
		this.ok.setEnabled(true);
		this.cancel.setEnabled(true);
		this.thread = null;
	}

	@Override
	public void run() {
		this.create();
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		
	}

}
